﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using Lucy.Filters;
using Lucy.Models;
using Lucy.DAL;

namespace Lucy.Controllers
{
    [InitializeSimpleMembership]
    public class OrderController : Controller
    {
        private LucyContext db = new LucyContext();

        //
        // GET: /Order/

        public ActionResult Index()
        {
            var orders = db.Orders.Include(o => o.Customer).Include(o => o.Diamond).Include(o => o.Jewel);
            return View(orders.ToList());
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id = 0)
        {
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "UserName");
            ViewBag.DiamondID = new SelectList(db.Diamonds, "DiamondID", "DiamondID");
            ViewBag.JewelId = new SelectList(db.Jewels, "JewelID", "JewelID");
            return View();
        }

        //
        // POST: /Order/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Order order)
        {
            if (ModelState.IsValid)
            {
                order.TotalPrice = db.Diamonds.Find(order.DiamondID).Price + db.Jewels.Find(order.JewelId).Price;
                order.Date = DateTime.Today;
                order.CustomerID = db.Customers.FirstOrDefault(u => u.UserName == User.Identity.Name).CustomerID;
                order.Status = "In progress";
                db.Orders.Add(order);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "UserName", order.CustomerID);
            ViewBag.DiamondID = new SelectList(db.Diamonds, "DiamondID", "DiamondID", order.DiamondID);
            ViewBag.JewelId = new SelectList(db.Jewels, "JewelID", "JewelID", order.JewelId);
            return View(order);
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "UserName", order.CustomerID);
            ViewBag.DiamondID = new SelectList(db.Diamonds, "DiamondID", "DiamondID", order.DiamondID);
            ViewBag.JewelId = new SelectList(db.Jewels, "JewelID", "JewelID", order.JewelId);
            return View(order);
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "UserName", order.CustomerID);
            ViewBag.DiamondID = new SelectList(db.Diamonds, "DiamondID", "DiamondID", order.DiamondID);
            ViewBag.JewelId = new SelectList(db.Jewels, "JewelID", "JewelID", order.JewelId);
            return View(order);
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Order order = db.Orders.Find(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

        //
        // POST: /Order/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Order order = db.Orders.Find(id);
            db.Orders.Remove(order);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}